package com.example.zhaohang.selfeapplication;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.zhaohang.utils.FragmenTwotUtils;

public class ChengeButtomButtonActivity extends BasicActivity implements View.OnClickListener {

    private LinearLayout   mHand;
    private LinearLayout   mLast;
    private RelativeLayout mMidle;
    private FrameLayout    mFrameLayout;
    private LinearLayout   mMidle_one;
    private LinearLayout   mMidle_two;

    private boolean isMiddle = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chenge_buttom_button);

        mHand = (LinearLayout) findViewById(R.id.hand);
        mLast = (LinearLayout) findViewById(R.id.last);
        mMidle = (RelativeLayout) findViewById(R.id.midle);
        mFrameLayout = (FrameLayout) findViewById(R.id.framelayout);

        mMidle_one = (LinearLayout) findViewById(R.id.midle_one);
        mMidle_two = (LinearLayout) findViewById(R.id.midle_two);

        mHand.setOnClickListener(this);
        mLast.setOnClickListener(this);
        mMidle.setOnClickListener(this);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(0));
        transaction.commit();

        mMidle_two.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(FragmenTwotUtils.provideFrage(0));
        transaction.hide(FragmenTwotUtils.provideFrage(1));
        transaction.hide(FragmenTwotUtils.provideFrage(2));
        transaction.hide(FragmenTwotUtils.provideFrage(3));
        switch (view.getId()) {
            case R.id.hand:
                if (!FragmenTwotUtils.provideFrage(0).isAdded()) {
                    transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(0));
                }
                transaction.show(FragmenTwotUtils.provideFrage(0));
                break;
            case R.id.last:
                if (!FragmenTwotUtils.provideFrage(3).isAdded()) {
                    transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(3));
                }
                transaction.show(FragmenTwotUtils.provideFrage(3));
                break;
            case R.id.midle:
                if (isMiddle) {
                    if (!FragmenTwotUtils.provideFrage(1).isAdded()) {
                        transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(1));
                    }
                    transaction.show(FragmenTwotUtils.provideFrage(1));
                } else {
                    if (!FragmenTwotUtils.provideFrage(2).isAdded()) {
                        transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(2));
                    }
                    transaction.show(FragmenTwotUtils.provideFrage(2));
                }
                break;
        }
        transaction.commit();
    }

    public void buttonchenge(View view) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(FragmenTwotUtils.provideFrage(0));
        transaction.hide(FragmenTwotUtils.provideFrage(1));
        transaction.hide(FragmenTwotUtils.provideFrage(2));
        transaction.hide(FragmenTwotUtils.provideFrage(3));
        if (isMiddle) {
            mMidle_two.setVisibility(View.VISIBLE);
            mMidle_one.setVisibility(View.GONE);
            if (!FragmenTwotUtils.provideFrage(2).isAdded()) {
                transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(2));
            }
            transaction.show(FragmenTwotUtils.provideFrage(2));
        } else {
            if (!FragmenTwotUtils.provideFrage(1).isAdded()) {
                transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(1));
            }
            transaction.show(FragmenTwotUtils.provideFrage(1));
            mMidle_two.setVisibility(View.GONE);
            mMidle_one.setVisibility(View.VISIBLE);
        }
        transaction.commit();
        isMiddle = !isMiddle;
    }

    //指定跳转到指定的Fragment中
    public void chengeFragmenta(int position){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(FragmenTwotUtils.provideFrage(0));
        transaction.hide(FragmenTwotUtils.provideFrage(1));
        transaction.hide(FragmenTwotUtils.provideFrage(2));
        transaction.hide(FragmenTwotUtils.provideFrage(3));
        if (!FragmenTwotUtils.provideFrage(position).isAdded()) {
            transaction.add(R.id.framelayout, FragmenTwotUtils.provideFrage(position));
        }
        transaction.show(FragmenTwotUtils.provideFrage(position));
        transaction.commit();
        //TODO:底部的button进行切换

    }
}
